/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import entity.Inventory;
import entity.MaterialRequired;
import entity.Product;
import entity.RawMaterial;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class ProductSessionBean implements ProductSessionBeanLocal, ProductSessionBeanRemote {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public void addInventoryRecord(Inventory i) {
        entityManager.persist(i);
        entityManager.flush();
    }

    @Override
    public void updateRawMaterial(RawMaterial r) {
        RawMaterial temp = getRawMaterial(r.getId());

        temp.setName(r.getName());
        temp.setDescription(r.getDescription());
        temp.setShelfLife(r.getShelfLife());
        temp.setLotSize(r.getLotSize());
        temp.setLeadTime(r.getLeadTime());
        temp.setStatus(r.getStatus());

        entityManager.persist(temp);
        entityManager.flush();
    }

    @Override
    public RawMaterial getRawMaterialByName(String name, Long id) {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterial r");

        for (Object o : query.getResultList()) {
            RawMaterial r = (RawMaterial) o;

            if (r.getName().equals(name) && r.getId() != id) {
                return r;
            }
        }

        return null;
    }

    @Override
    public void deleteRawMaterial(Long id) {
        RawMaterial rawMaterial = entityManager.find(RawMaterial.class, id);

        entityManager.remove(rawMaterial);
        entityManager.flush();
    }

    @Override
    public void deleteProduct(String code) {
        Product product = entityManager.find(Product.class, code);

        entityManager.remove(product);
        entityManager.flush();
    }

    @Override
    public void deleteProductMaterial(String code, Long id) {
        Product product = entityManager.find(Product.class, code);
        MaterialRequired temp2 = entityManager.find(MaterialRequired.class, id);

        product.getMaterialRequired().remove(temp2);

        entityManager.persist(product);

        entityManager.remove(temp2);
    }

    @Override
    public void updateProduct(Product p) {
        Product temp = getProduct(p.getCode());

        temp.setName(p.getName());
        temp.setCapacityPerHour(p.getCapacityPerHour());
        temp.setDescription(p.getDescription());
        temp.setPrice(p.getPrice());
        temp.setStatus(p.getStatus());
        temp.setWeight(p.getWeight());

        entityManager.persist(temp);
        entityManager.flush();
    }

    @Override
    public void updateProduct(String code, MaterialRequired materialRequired) {
        Product product = entityManager.find(Product.class, code);

        List<MaterialRequired> temp = product.getMaterialRequired();

        RawMaterial rawMaterial = entityManager.find(RawMaterial.class, materialRequired.getRawMaterial().getId());

        materialRequired.setRawMaterial(rawMaterial);

        temp.add(materialRequired);

        product.setMaterialRequired(temp);

        entityManager.persist(product);
        entityManager.flush();
    }

    @Override
    public List<Product> getProducts() {
        Query query = entityManager.createQuery("SELECT p FROM Product p");
        ArrayList<Product> products = new ArrayList<Product>();

        for (Object o : query.getResultList()) {
            Product p = (Product) o;
            products.add(p);
        }

        return products;
    }

    @Override
    public Product getProductByName(String name, String code) {
        Query query = entityManager.createQuery("SELECT p FROM Product p");

        for (Object o : query.getResultList()) {
            Product p = (Product) o;

            if (p.getName().equals(name) && !p.getCode().equals(code)) {
                return p;
            }
        }

        return null;
    }

    @Override
    public Product getProduct(String code) {
        Product p = entityManager.find(Product.class, code);

        return p;
    }

    @Override
    public Product getProduct(String code, String name) {
        Query query = entityManager.createQuery("SELECT p FROM Product p");

        for (Object o : query.getResultList()) {
            Product p = (Product) o;

            if (p.getName().equals(name) || p.getCode().equals(code)) {
                return p;
            }
        }

        return null;
    }

    @Override
    public void addProduct(Product product) {
        List<MaterialRequired> temp = new ArrayList<MaterialRequired>();

        for (MaterialRequired materialRequired : product.getMaterialRequired()) {
            RawMaterial rawMaterial = getRawMaterial(materialRequired.getRawMaterial().getId());

            materialRequired.setRawMaterial(rawMaterial);

            temp.add(materialRequired);
        }

        product.setMaterialRequired(temp);

        entityManager.persist(product);
        entityManager.flush();
    }

    @Override
    public RawMaterial getRawMaterial(String name) {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterial r");

        for (Object o : query.getResultList()) {
            RawMaterial rm = (RawMaterial) o;

            if (rm.getName().equals(name)) {
                return rm;
            }
        }

        return null;
    }

    @Override
    public void addRawMaterial(RawMaterial rawMaterial) {
        entityManager.persist(rawMaterial);
        entityManager.flush();
    }

    @Override
    public RawMaterial getRawMaterial(Long id) {
        RawMaterial rawMaterial = entityManager.find(RawMaterial.class, id);

        return rawMaterial;
    }

    @Override
    public List<RawMaterial> getRawMaterials() {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterial r ORDER BY r.name ASC");
        List<RawMaterial> rawMaterials = new ArrayList<RawMaterial>();

        for (Object o : query.getResultList()) {
            RawMaterial rawMaterial = (RawMaterial) o;

            rawMaterials.add(rawMaterial);
        }

        return rawMaterials;
    }
    
     @Override    
      public void tempDeleteProducts(String productId){
    
        Product product = entityManager.find(Product.class, productId);
        entityManager.remove(product);

        entityManager.flush();
    
    }

}
